package com.jmemoir.base.async.controller;

import com.jmemoir.base.async.AsyncTestService;
import com.jmemoir.base.common.dto.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 测试控制层
 *
 * @author Tellsea
 * @date 2023/8/2
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/baseAsyncTest")
public class BaseAsyncTestController {

    private final AsyncTestService asyncTestService;

    /**
     * 测试异步业务
     *
     * @return
     */
    @GetMapping("/handleBusiness")
    public Result handleBusiness() {
        log.info("执行前");
        asyncTestService.handleBusiness();
        log.info("执行后");
        return Result.success("测试成功");
    }

    /**
     * 测试异步有返回值
     *
     * @return
     */
    @GetMapping("/hasReturn")
    public Result hasReturn() throws ExecutionException, InterruptedException {
        log.info("执行前");
        Future<String> future =  asyncTestService.hasReturn();
        log.info(future.get());
        log.info("执行后");
        return Result.success("测试成功");
    }
}
